SaGetStateVar()
and SaSetStateVar()
.To operate this, you first request a cookie, and then write into it. Later on you can retrieve it from the State Server by specifying the number. Finally, you can delete the cookie. The cookie ID number assigned by Sapphire/Web has no meaning, and can therefore be used as a safeguard against prying eyes.
For further information on the Sapphire/Web API calls used here, please refer to the Sapphire/Web Reference Manual, Chapter 2.
You may find that you can use a state server installation on another machine successfully, but not one on your own machine.
When you press Submit, the listed cookie number you supplied will be queried for a state server value. If it is found, the "String to Append" value will be appended to the value of that cookie and re-saved. If it is not found, 2 things can happen. If a String to Append is given, a cookie will be created with your String to Append as the saved value. The cookie number created depends on your input cookie number. If it is 0 or invalid (text), then the next available cookie will be retrieved. If it is >0 and valid, then a cookie will be created with that number. If no String to Append is given, no cookie will be created, and you will be notified that no such cookie exists.
If you do not have the State Server running you will get a cookie value of minus one.
In the following sections you add code to your project. If you see the following lines already present in the code area you are modifying, do not place your code between them, as it will be lost.
/***** Begin Site Registry Code *****/ /***** End of Site Registry Code *****/Sapphire/Web considers that it "owns" any code between these two comments, and that it has the "right" to alter it as it sees fit.
<A HREF="URL" >Go to Cookie reviewer</A>
Cookie Number = ##Sa_cooknum## Cookie Value = ##Sa_cookval## <BR><P> <FORM ACTION="FormAction" METHOD="post" > Cookie Number? : <INPUT NAME="GetCookNum" VALUE="" SIZE=20><BR> String to Append? : <INPUT NAME="GetAppendage" VALUE="" SIZE=20><BR> <INPUT TYPE="SUBMIT" VALUE="Submit"><INPUT TYPE="RESET" VALUE="Reset"> <INPUT TYPE="hidden" NAME="SaFormName" VALUE="FormAction__Fpage2_html"> </FORM>
Step 1 - right-click the URL activator for home.html and select Code.
#include "sacook.h" int cooknum; char *g_cooknum, *g_cookval; char buff[100]; char *tmp;
SaRegisterStringSite("page2.html", "cooknum", "N/A"); SaRegisterStringSite("page2.html", "cookval", "N/A");Save and close the code window.
Step 2 - Right-click the FormAction activator for page2.html and select Code.
#include "sacook.h" int cooknum; char *s_cooknum, *s_cookval; char buff[100]; char *tmp, *tmp2;
tmp = SaGetInputValue("GetCookNum"); tmp2 = SaGetInputValue("GetAppendage"); cooknum = atoi(tmp); sprintf(buff, "%d",cooknum); s_cooknum = SaCopyString(buff); s_cookval = SaGetCookie(cooknum); if (!s_cookval) { if (strlen(tmp2) > 0) { if (cooknum > 0) SaInternStringCookie(tmp2, cooknum); else cooknum = SaInternString(tmp2); s_cookval = tmp2; } else { s_cookval = "Cookie Not Set!"; sprintf(buff, "%d",cooknum); s_cooknum = SaCopyString(buff); } } else { s_cookval = SaStrMCat(s_cookval, tmp2, NULL); SaUpdateCookie(s_cookval, cooknum); } SaRegisterStringSite("page2.html", "cooknum", s_cooknum); SaRegisterStringSite("page2.html", "cookval", s_cookval);Save your code, save the project, and test it.
Click on the Submit button. This will submit a query for a non-existent cookie number (blank). You will get this:
The screen above shows that the code has submitted a request for a cookie, found that no value was associated with that cookie, and that no cookie number had been asked for in the first place, so stored the string and received a cookie number back. This should be the next available number within the state server.
This time, put the example value "qwerty" into the "String to Append" type-in box and press Submit.
Append a string to the content of that cookie. Enter the number returned as the cookie ID (400000 in the instance above), and specify the concatenation of "uiop". You'll see the cookie number used and the concatenated string.